package com.yuqing.sentimentapp.home.presenter.impl;

import android.content.Context;

import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.home.bean.IssuedPrepareDataBean;
import com.yuqing.sentimentapp.home.bean.IssuedSaveRegionDataBean;
import com.yuqing.sentimentapp.home.presenter.IssuedPresenter;
import com.yuqing.sentimentapp.home.view.IssuedView;
import com.yuqing.sentimentapp.module.ModuleManager;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ShenHe on 2019/3/14.
 */

public class IssuedPresenterImpl implements IssuedPresenter {
    private Context mContext;
    private IssuedView issuedView;

    public IssuedPresenterImpl(Context context, IssuedView issuedView) {
        this.mContext = context;
        this.issuedView = issuedView;
    }

    @Override
    public void loadIssuedPrepareRequest(String articleId, String subDeptCode, int position) {
        ModuleManager.getInstance().loadIssuedPrepareRequest(mContext, articleId, subDeptCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<IssuedPrepareDataBean>() {
                    @Override
                    protected void hideDialog() {
                        ((BaseActivity) mContext).stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        ((BaseActivity) mContext).startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(IssuedPrepareDataBean bean) {
                        if (checkContext()) return;
                        issuedView.returnIssuedPrepareSuccess(bean, articleId, position);
                    }

                    @Override
                    protected void onFailure(String message) {
                        issuedView.onFailure(message);
                    }
                });
    }

    @Override
    public void saveIssuedRegionRequest(String articleId, String province, String city, String county, int position) {
        ModuleManager.getInstance().saveIssuedRegionRequest(mContext, articleId, province, city, county)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<IssuedSaveRegionDataBean>() {
                    @Override
                    protected void hideDialog() {
                        ((BaseActivity) mContext).stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        ((BaseActivity) mContext).startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(IssuedSaveRegionDataBean bean) {
                        if (checkContext()) return;
                        issuedView.returnSaveRegionResultSuccess(bean, articleId, position);
                    }

                    @Override
                    protected void onFailure(String message) {
                        issuedView.onFailure(message);
                    }
                });
    }

    private boolean checkContext() {
        if (mContext == null || ((BaseActivity) mContext).isFinishing() || ((BaseActivity) mContext).isDestroyed())
            return true;
        return false;
    }
}
